using System;
using System.Collections.Generic;
using UnityEngine;

public class GarageUI : MonoBehaviour
{
	private GameObject uiobj;

	public GameObject[] PlanePrefab;

	public PosRot[] PlanePos;

	private GameObject planeobj;

	public UIEventListener rankbtn;

	public UISprite currankflag;

	public UILabel currankname;

	public UILabel planenamelabel;

	public List<UISprite> scrollsp;

	public List<UILabel> valuelabel;

	public UISprite needrankflag;

	public UIEventListener needrankbtn;

	public UISprite[] weaponflags;

	public UIEventListener[] weaponbtn;

	public UISprite[] twoweaponflag;

	private DataSave_Plane curplanedata;

	private plane_buy planebuydata;

	public UIWidget buywi;

	public UIWidget upgradewi;

	public UISprite buyflag;

	public UISprite lockflag;

	public UISprite buybtnflag;

	public UILabel buynumlabel;

	public UIEventListener buybtn;

	public UIEventListener upgradebtn;

	public UIEventListener piecebtn;

	public TweenPosition downlineAnima;

	public TweenPosition leftAnima;

	public TweenPosition rightAnima;

	public TweenAlpha showAnima;

	public UIEventListener showArea;

	public UIEventListener backshowbtn;

	public UIEventListener lastbtn;

	public UIWidget lastwi;

	public UIEventListener nextbtn;

	public UIWidget nextwi;

	public UIScrollBar planescroll;

	public UIScrollView planescrollview;

	public List<UISprite> planelock;

	public List<UIEventListener> planebtn;

	public Transform selecttra;

	private bool scrollmoveflag;

	private float starttime;

	private float intaltime;

	private float endval;

	private float scrollrate;

	private float scrollstep = 0.2f;

	private int[] speed = new int[10]
	{
		160,
		172,
		176,
		180,
		186,
		188,
		192,
		194,
		198,
		200
	};

	private int[] steer = new int[10]
	{
		20,
		26,
		34,
		39,
		46,
		54,
		59,
		64,
		70,
		76
	};

	private int[] handing = new int[10]
	{
		30,
		32,
		35,
		39,
		41,
		46,
		52,
		58,
		65,
		70
	};

	private float[] hpscroll = new float[10]
	{
		0.3f,
		0.35f,
		0.4f,
		0.45f,
		0.5f,
		0.55f,
		0.65f,
		0.75f,
		0.85f,
		0.95f
	};

	private float[] speedscroll = new float[10]
	{
		0.2f,
		0.3f,
		0.4f,
		0.45f,
		0.52f,
		0.6f,
		0.75f,
		0.8f,
		0.85f,
		0.9f
	};

	private float[] steeringscroll = new float[10]
	{
		0.5f,
		0.55f,
		0.6f,
		0.65f,
		0.7f,
		0.75f,
		0.8f,
		0.85f,
		0.9f,
		0.95f
	};

	private float[] handingscroll = new float[10]
	{
		0.3f,
		0.4f,
		0.45f,
		0.48f,
		0.52f,
		0.55f,
		0.6f,
		0.65f,
		0.7f,
		0.8f
	};

	private bool showinfoscroll;

	private float time_showinfo;

	private PieceData curpiece;

	public UIWidget confirmwi;

	public Animator animator;
	//public Animation confirmani;

	public UIEventListener okbtn;

	public UIEventListener closebtn;

	public UISprite spendflag;

	public UILabel spendnumlabel;

	public UISprite[] hintflags;

	public UIWidget hotsalewi;

	public UIEventListener hotsalebtn;

	public UISprite[] forstars;

	private expdata curexpdata;

	private bool isshowconform;

	private string[] weapoonname = new string[5]
	{
		"护甲",
		"机枪",
		"远程导弹",
		"多目标导弹",
		"导弹诱饵"
	};

	private int curselect = -1;

	private void Start()
	{
		for (int i = 0; i < planebtn.Count; i++)
		{
			UIEventListener uIEventListener = planebtn[i];
			uIEventListener.onClick = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener.onClick, new UIEventListener.VoidDelegate(OnClickPlaneBtn));
		}
		rankbtn.onClick = OnClickjunxianBtn;
		lastbtn.onClick = OnClicklastBtn;
		nextbtn.onClick = OnClicknextBtn;
		buybtn.onClick = OnClickBuyBtn;
		UIEventListener uIEventListener2 = upgradebtn;
		uIEventListener2.onClick = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener2.onClick, new UIEventListener.VoidDelegate(OnClickupgradeBtn));
		okbtn.onClick = OnClickBuyOKbtn;
		closebtn.onClick = OnClickclosebtn;
		showArea.onDrag = OnDragshowarea;
		backshowbtn.onClick = backshowarea;
		piecebtn.onClick = OnClickpiecebtn;
		needrankbtn.onClick = OnClikneedrankbtn;
		for (int j = 0; j < weaponbtn.Length; j++)
		{
			weaponbtn[j].onClick = OnClickweaponBtn;
		}
		hotsalebtn.onClick = OnClickhotsalebtn;
	}

	public void init_ui_info()
	{
		personinfo_show();
		MenuPlayerprefs.instance.readStoreData();
		initscroll();
		if (MenuPlayerprefs.instance.getmultipleisunlock())
		{
			twoweaponflag[0].color = Color.white;
		}
		else
		{
			twoweaponflag[0].color = Color.black;
		}
		if (MenuPlayerprefs.instance.getmissisunlock())
		{
			twoweaponflag[1].color = Color.white;
		}
		else
		{
			twoweaponflag[1].color = Color.black;
		}
		if (MenuPlayerprefs.instance.menufirst == 2)
		{
			MenuUIController.instance.enable_guideui(isone: false);
		}
		show_hintflag(curexpdata.level);
	}

	private void Update()
	{
		if (scrollmoveflag)
		{
			if (planescrollview.isbepress)
			{
				scrollmoveflag = false;
				return;
			}
			if (Time.time - starttime < intaltime)
			{
				planescroll.value += Time.deltaTime * scrollrate;
				if (Mathf.Abs(planescroll.value - endval) > scrollstep)
				{
					scrollmoveflag = false;
				}
			}
			else
			{
				planescroll.value = endval;
				scrollmoveflag = false;
			}
		}
		if (showinfoscroll)
		{
			time_showinfo += Time.deltaTime;
			scrollsp[0].fillAmount = Mathf.Lerp(scrollsp[0].fillAmount, hpscroll[curselect], 5f * Time.deltaTime);
			scrollsp[1].fillAmount = Mathf.Lerp(scrollsp[1].fillAmount, speedscroll[curselect], 5f * Time.deltaTime);
			scrollsp[2].fillAmount = Mathf.Lerp(scrollsp[2].fillAmount, steeringscroll[curselect], 5f * Time.deltaTime);
			scrollsp[3].fillAmount = Mathf.Lerp(scrollsp[3].fillAmount, handingscroll[curselect], 5f * Time.deltaTime);
			if (time_showinfo > 1f)
			{
				showinfoscroll = false;
				time_showinfo = 0f;
			}
		}
	}

	public bool show_hintflag(int ranklevel)
	{
		bool result = false;
		for (int i = 0; i < 10; i++)
		{
			planebuydata = MenuDataController.instance.PlaneData[i];
			if (ranklevel >= planebuydata.unlocklevel && MenuPlayerprefs.instance.planehint[i] == 1)
			{
				hintflags[i].enabled = true;
				result = true;
			}
			else
			{
				hintflags[i].enabled = false;
			}
		}
		return result;
	}

	public void planeinfo_show(int planeindex)
	{
		curplanedata = MenuPlayerprefs.instance.DS_Plane[planeindex];
		planebuydata = MenuDataController.instance.PlaneData[planeindex];
		planenamelabel.text = planebuydata.name;
		if (planeobj != null)
		{
			UnityEngine.Object.DestroyObject(planeobj);
		}
		planeobj = (UnityEngine.Object.Instantiate(PlanePrefab[planeindex]) as GameObject);
		planeobj.transform.position = PlanePos[planeindex].pos;
		planeobj.transform.rotation = Quaternion.Euler(PlanePos[planeindex].rot);
		if (curplanedata.islock == 0)
		{
			buywi.alpha = 0f;
			buywi.gameObject.SetActive(false);
			upgradewi.alpha = 1f;
			upgradewi.gameObject.SetActive(true);
			hotsalewi.alpha = 0f;
			hotsalewi.gameObject.SetActive(false);
			spendnumlabel.gameObject.SetActive(false);
			buynumlabel.gameObject.SetActive(false);
			buybtnflag.gameObject.SetActive(false);
			upgradewi.gameObject.SetActive(true);
		}
		else
		{
			spendnumlabel.gameObject.SetActive(true);
			buynumlabel.gameObject.SetActive(true);
			buybtnflag.gameObject.SetActive(true);
			upgradewi.gameObject.SetActive(false);
			if (curexpdata.level >= planebuydata.unlocklevel)
			{
				buybtnflag.spriteName = "JS_anNiu";
				lockflag.enabled = false;
			}
			else
			{
				buybtnflag.spriteName = "JS_anNiuHui";
				lockflag.enabled = true;
			}
			buywi.alpha = 1f;
			upgradewi.alpha = 0f;
			buywi.gameObject.SetActive(true);
			upgradewi.gameObject.SetActive(false);
			switch (planebuydata.unlocktype)
			{
				case 0:
					buyflag.spriteName = "GY_zuanShi_01";
					buyflag.SetDimensions(32, 24);
					spendflag.spriteName = "GY_zuanShi_01";
					spendflag.SetDimensions(32, 24);
					buynumlabel.text = string.Empty + planebuydata.unlocknum;
					spendnumlabel.text = string.Empty + planebuydata.unlocknum;
					break;
				case 1:
					buyflag.spriteName = "CZ_jinBi_02";
					buyflag.SetDimensions(32, 25);
					spendflag.spriteName = "CZ_jinBi_02";
					spendflag.SetDimensions(32, 25);
					buynumlabel.text = string.Empty + planebuydata.unlocknum;
					spendnumlabel.text = string.Empty + planebuydata.unlocknum;
					break;
				case 2:
					curpiece = MenuDataController.instance.Getpiece(planebuydata.id);
					buyflag.spriteName = "GY_SP_" + curpiece.id;
					buyflag.SetDimensions(36, 33);
					spendflag.spriteName = "GY_SP_" + curpiece.id;
					spendflag.SetDimensions(36, 33);
					buynumlabel.text = MenuPlayerprefs.instance.piecenum[curpiece.id] + "/" + planebuydata.unlocknum;
					spendnumlabel.text = MenuPlayerprefs.instance.piecenum[curpiece.id] + "/" + planebuydata.unlocknum;
					break;
			}

			if ((planeindex == 2 && MenuPlayerprefs.instance.saleid == 0) || (planeindex == 5 && MenuPlayerprefs.instance.saleid == 2))
			{
				hotsalewi.alpha = 1f;
				hotsalewi.gameObject.SetActive(true);
			}
			else
			{
				hotsalewi.alpha = 0f;
				hotsalewi.gameObject.SetActive(false);
			}
		}
		valuelabel[0].text = string.Empty + planebuydata.hp;
		valuelabel[1].text = string.Empty + (int)((float)speed[planeindex] * 3.6f);
		valuelabel[2].text = string.Empty + steer[planeindex];
		valuelabel[3].text = string.Empty + handing[planeindex];
		showinfoscroll = true;
		time_showinfo = 0f;
		for (int i = 0; i < 5; i++)
		{
			if (i + 1 <= planebuydata.weapons)
			{
				weaponflags[i].alpha = 1f;
				weaponflags[i].gameObject.SetActive(true);
			}
			else
			{
				weaponflags[i].alpha = 0f;
				weaponflags[i].gameObject.SetActive(false);
			}
		}
		if (planebuydata.unlocklevel == -1)
		{
			needrankflag.spriteName = "1." + MenuPlayerprefs.instance.getplayername(0);
			needrankflag.SetDimensions(30, 33);
		}
		else
		{
			needrankflag.spriteName = planebuydata.unlocklevel + "." + MenuPlayerprefs.instance.getplayername(planebuydata.unlocklevel - 1);
			if (planebuydata.unlocklevel >= 15)
			{
				needrankflag.SetDimensions(40, 34);
			}
			else
			{
				needrankflag.SetDimensions(30, 33);
			}
		}
		show_star();
	}

	public void show_star()
	{
		int num = (int)(planebuydata.star * 10f);
		int num2 = num / 10;
		bool flag = (num % 10 != 0) ? true : false;
		for (int i = 0; i < forstars.Length; i++)
		{
			if (i < num2)
			{
				forstars[i].spriteName = "JS_xing2";
				forstars[i].enabled = true;
			}
			else if (i == num2 && flag)
			{
				forstars[i].spriteName = "JS_banXing";
				forstars[i].enabled = true;
			}
			else
			{
				forstars[i].enabled = false;
			}
		}
	}

	public void initscroll()
	{
		for (int i = 0; i < 10; i++)
		{
			if (MenuPlayerprefs.instance.DS_Plane[i].islock == 1)
			{
				planelock[i].enabled = true;
			}
			else
			{
				planelock[i].enabled = false;
			}
		}
		planescroll.value = (float)(curselect - 2) / 7f;
	}

	public void personinfo_show()
	{
		curexpdata = MenuPlayerprefs.instance.GetExp();
		if (curexpdata.level >= 19)
		{
			curexpdata.level = 19;

			currankflag.spriteName = "19." + MenuPlayerprefs.instance.ranknameImage[curexpdata.level - 1];
			currankname.text = curexpdata.name;
			currankflag.SetDimensions(56, 48);
			return;
		}
		currankflag.spriteName = string.Empty + curexpdata.level + "." + MenuPlayerprefs.instance.ranknameImage[curexpdata.level - 1];
		currankname.text = curexpdata.name;
		if (curexpdata.level >= 15)
		{
			currankflag.SetDimensions(56, 48);
		}
		else
		{
			currankflag.SetDimensions(40, 44);
		}
	}

	public void ChangeScroll()
	{
		if (planescroll.value < 0.01f)
		{
			lastwi.alpha = 0f; lastwi.gameObject.SetActive(false);
		}
		else if (lastwi.alpha != 1f)
		{
			lastwi.alpha = 1f; lastwi.gameObject.SetActive(true);
		}
		if (planescroll.value > 0.99f)
		{
			nextwi.alpha = 0f; nextwi.gameObject.SetActive(false);
		}
		else if (nextwi.alpha != 1f)
		{
			nextwi.alpha = 1f; nextwi.gameObject.SetActive(true);
		}
	}

	public void OnClickupgradeBtn(GameObject btn)
	{
		MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
		disable(isreset: false);
		MenuUIController.instance.enable_upgradeui(curselect);
	}

	public void OnClickBuyBtn(GameObject btn)
	{
		if (curexpdata.level >= planebuydata.unlocklevel)
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
			isshowconform = true;
			confirmwi.gameObject.SetActive(true);
			confirmwi.alpha = 1f;
			/* confirmani["PopWinAnimation"].time = 0f;
			confirmani["PopWinAnimation"].speed = 1f;
			confirmani.Play(); */
			animator.SetTrigger("popWin1");
			MenuBackController.instance.ChangeMenuUIstate(MenuUIState.confirm_buyplane);
		}
		else
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_pophit, 0f);
			MenuUIController.instance.enable_hitinfoui(hitinfoid.rank, MenuPlayerprefs.instance.getplayernameCN(planebuydata.unlocklevel - 1));
		}
	}

	public void OnClikneedrankbtn(GameObject btn)
	{
		if (planebuydata.unlocklevel == -1)
		{
			MenuUIController.instance.enable_hitinfoui(hitinfoid.rank, MenuPlayerprefs.instance.getplayernameCN(0));
		}
		else
		{
			MenuUIController.instance.enable_hitinfoui(hitinfoid.rank, MenuPlayerprefs.instance.getplayernameCN(planebuydata.unlocklevel - 1));
		}
	}

	public void OnClickweaponBtn(GameObject btn)
	{
		switch (btn.name)
		{
			case "weapon0":
				MenuUIController.instance.enable_hitinfoui(hitinfoid.garageweapon, weapoonname[0]);
				break;
			case "weapon1":
				MenuUIController.instance.enable_hitinfoui(hitinfoid.garageweapon, weapoonname[1]);
				break;
			case "weapon2":
				MenuUIController.instance.enable_hitinfoui(hitinfoid.garageweapon, weapoonname[2]);
				break;
			case "weapon3":
				MenuUIController.instance.enable_hitinfoui(hitinfoid.garageweapon, weapoonname[3]);
				break;
			case "weapon4":
				MenuUIController.instance.enable_hitinfoui(hitinfoid.garageweapon, weapoonname[4]);
				break;
		}
	}

	public void OnClickBuyOKbtn(GameObject btn)
	{
		switch (planebuydata.unlocktype)
		{
			case 0:
				if (MenuPlayerprefs.instance.DecDiamond(planebuydata.unlocknum))
				{
					MonoInstance<AudioController>.instance.play(AudioType.UI_BUY, 0f);
					MenuPlayerprefs.instance.save_plane_lock(curselect);
					planelock[curselect].enabled = false;
					planeinfo_show(curselect);
					MenuBackController.instance.BackMenuUIstate();
				}
				else
				{
					MonoInstance<AudioController>.instance.play(AudioType.UI_BTN1, 0f);
					MenuUIController.instance.enable_chargeui(1);
				}
				break;
			case 1:
				if (MenuPlayerprefs.instance.DecMoney(planebuydata.unlocknum))
				{
					MonoInstance<AudioController>.instance.play(AudioType.UI_BUY, 0f);
					MenuPlayerprefs.instance.save_plane_lock(curselect);
					planelock[curselect].enabled = false;
					planeinfo_show(curselect);
					MenuBackController.instance.BackMenuUIstate();
				}
				else
				{
					GameObject.Find("PopupConfirm").SetActive(false);
					MonoInstance<AudioController>.instance.play(AudioType.UI_BTN1, 0f);
					MenuUIController.instance.enable_chargeui(0);
				}
				break;
			case 2:
				if (MenuPlayerprefs.instance.DecPiece(curpiece.id, planebuydata.unlocknum))
				{
					MonoInstance<AudioController>.instance.play(AudioType.UI_BUY, 0f);
					MenuPlayerprefs.instance.save_plane_lock(curselect);
					MenuPlayerprefs.instance.savepieceflag(curpiece.id);
					planelock[curselect].enabled = false;
					planeinfo_show(curselect);
					MenuBackController.instance.BackMenuUIstate();
				}
				else
				{
					MonoInstance<AudioController>.instance.play(AudioType.UI_pophit, 0f);
					MenuUIController.instance.enable_getpieceui(curpiece);
				}
				break;
		}
		confirmwi.alpha = 0f; confirmwi.gameObject.SetActive(false);
	}

	public void OnClickpiecebtn(GameObject btn)
	{
		if (planebuydata.unlocktype == 2)
		{
			MenuUIController.instance.enable_getpieceui(curpiece);
			MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
		}
	}

	public void OnClickclosebtn(GameObject btn)
	{
		if (isshowconform)
		{
			isshowconform = false;
			MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
			/* confirmani["PopWinAnimation"].time = confirmani.clip.length;
			confirmani["PopWinAnimation"].speed = -1f;
			confirmani.Play(); */
			animator.SetTrigger("popWin2");
			Invoke("confirmClose", animator.runtimeAnimatorController.animationClips[0].length);
		}
	}

	public void confirmClose()
	{
		confirmwi.gameObject.SetActive(false);
		MenuBackController.instance.BackMenuUIstate();
	}

	public void OnClicklastBtn(GameObject btn)
	{
		if (!scrollmoveflag && planescroll.value != 0f)
		{
			scrollmoveflag = true;
			MonoInstance<AudioController>.instance.play(AudioType.UI_BTN1, 0f);
			starttime = Time.time;
			intaltime = 0.5f;
			scrollrate = -0.4f;
			endval = planescroll.value - scrollstep;
			if (planescroll.value < scrollstep)
			{
				intaltime = planescroll.value / scrollstep * intaltime;
				endval = 0f;
			}
		}
	}

	public void OnClicknextBtn(GameObject btn)
	{
		if (!scrollmoveflag && planescroll.value != 1f)
		{
			scrollmoveflag = true;
			MonoInstance<AudioController>.instance.play(AudioType.UI_BTN1, 0f);
			starttime = Time.time;
			intaltime = 0.5f;
			scrollrate = 0.4f;
			endval = planescroll.value + scrollstep;
			if (1f - planescroll.value < scrollstep)
			{
				intaltime = (1f - planescroll.value) / scrollstep * intaltime;
				endval = 1f;
			}
		}
	}

	public void OnClickPlaneBtn(GameObject btn)
	{
		int num = 0;
		while (true)
		{
			if (num < planebtn.Count)
			{
				if (btn.name.Equals("item" + num))
				{
					break;
				}
				num++;
				continue;
			}
			return;
		}
		clickplaneitem(num);
	}

	public void clickplaneitem(int index)
	{
		if (curselect != index)
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
			selecttra.parent = planebtn[index].transform;
			selecttra.localPosition = Vector3.zero;
			curselect = index;
			planeinfo_show(index);
			if (hintflags[index].enabled)
			{
				//hintflags[index].enabled = false;
				MenuPlayerprefs.instance.set_planehint(index);
			}
		}
	}

	public void OnDragshowarea(GameObject btn, Vector2 delta)
	{
		if (MenuBackController.instance.curstate != MenuUIState.showplaneui && Vector2.SqrMagnitude(delta) >= 64f * GlobalInf.ratioReltoUI)
		{
			MenuBackController.instance.ChangeMenuUIstate(MenuUIState.showplaneui);
			MenuUIController.instance.uplineui.AnimaPlayF();
			downlineAnima.PlayForward();
			leftAnima.PlayForward();
			rightAnima.PlayForward();
			showAnima.PlayForward();
			MenuUIController.instance.jikucam.startfun();
		}
	}

	public void backshowarea(GameObject btn)
	{
		MonoInstance<AudioController>.instance.play(AudioType.UI_BTN1, 0f);
		MenuBackController.instance.BackMenuUIstate();
		MenuUIController.instance.uplineui.AnimaPlayR();
		downlineAnima.PlayReverse();
		leftAnima.PlayReverse();
		rightAnima.PlayReverse();
		showAnima.PlayReverse();
		MenuUIController.instance.jikucam.ResetRotate();
	}

	public void OnClickjunxianBtn(GameObject btn)
	{
		MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
		MenuUIController.instance.enable_junxianui();
	}

	public void OnClickhotsalebtn(GameObject btn)
	{
		MenuUIController.instance.AutoSaleui();
	}

	public void enable(int num)
	{
		if (uiobj == null)
		{
			uiobj = base.gameObject;
		}
		NGUITools.SetActive(uiobj, state: true);
		if (curselect == -1)
		{
			curselect = num;
		}
		planeinfo_show(curselect);
		selecttra.parent = planebtn[curselect].transform;
		selecttra.localPosition = Vector3.zero;
		init_ui_info();
	}

	public void disable(bool isreset)
	{
		if (isreset)
		{
			curselect = -1;
		}
		NGUITools.SetActive(uiobj, state: false);
		if (planeobj != null)
		{
			UnityEngine.Object.DestroyObject(planeobj);
		}
	}
}
